package com.ghost.manager
{
	import com.ghost.model.Global_data;
	import com.ghost.model.Global_ui_data;
	import com.ghost.model.Loading_data;
	import com.ghost.model.Object_pool;
	import com.ghost.utils.Debug;
	import com.ghost.utils.GC;
	import com.ghost.view.Loading.Loading_big_mc;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.media.Sound;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	/**
	 * v1.1
	 * @author ghost
	 * 加载管理
	 */	
	public class Load_manager
	{
		private static var instance:Load_manager;
		public static function get_Instance():Load_manager
		{
			if(instance == null)
			{
				instance  = new Load_manager();
			}
			return instance;
		}
		private var loading_bool:Boolean;//是否正在加载中
		private var load_arr:Vector.<Object>;//存放要加载的数据
		private var ld:Loader;//实体加载容器
		private var urlld:URLLoader;//数据加载容器
		private var first_bool:Boolean = true;//是否初始加载 
		private var type:String;//加载数据类型
		private var loading_big_mc:Loading_big_mc;//loading条
		private var sound:Sound;//声音实例
		public function Load_manager()
		{
			load_arr = new Vector.<Object>();
		}
		/**
		 * @param url_s 资源地址
		 * @param type_s 资源数据类型
		 * @param des_str 描述性提示
		 * @param call_back 加载完成回调函数
		 * @param need_decode 是否需要解密
		 */		
		public function add(url_s:String,type_s:String,des_str:String='',call_back:Function=null,need_decode:Boolean=false):void
		{
			load_arr.push({url:url_s,type:type_s,des:des_str,callback:call_back,need_decode:need_decode});
		}
		public function start():void
		{
			if(!loading_bool)
			{
				real_start();
			}
		}
		private function real_start():void
		{
			trace(load_arr[0].url,load_arr[0].des)
			Debug.log(load_arr[0].url);
			if(!Object_pool.loaded_obj_pool.containsKey(load_arr[0].url))
			{
				if(first_bool)
				{
					// 第一次进行加载发事件显示loading.........
					if(!Object_pool.loaded_obj_pool.containsKey(Loading_big_mc))
					{
						loading_big_mc = new Loading_big_mc();
						Object_pool.loaded_obj_pool.put(Loading_big_mc,loading_big_mc);
					}
					else loading_big_mc = Object_pool.loaded_obj_pool.get(Loading_big_mc);
					Pop_win_manager.getInstance().Creat_pop_win(loading_big_mc,false);
				}
				type = load_arr[0].type;
				loading_bool = true;
				switch(type)
				{
					case Loading_data.type_xml:
					{
						data_ld();
						break;
					}
					case Loading_data.type_sound:
					{
						sound_ld();
						break;
					}
					case Loading_data.type_video:
					{
						vedio_ld();
						break;
					}
					default:
					{
						content_ld();
						break;
					}
				}
				first_bool = false;
			}
			else
			{
				end_init();
			}
		}
		//加载数据
		private function data_ld():void
		{
			urlld = null;
			GC.nowGC();
			if(!urlld)urlld = new URLLoader();
			if(load_arr[0].need_decode)urlld.dataFormat = URLLoaderDataFormat.BINARY;
			urlld.load(new URLRequest(load_arr[0].url));
			urlld.addEventListener(Event.COMPLETE,url_cHandler);
			urlld.addEventListener(ProgressEvent.PROGRESS,pro_Handler);
			urlld.addEventListener(IOErrorEvent.IO_ERROR,io_Handler);
		}
		//加载声音mp3 没有用了.
		private function sound_ld():void
		{
			sound = new Sound();
			sound.load(new URLRequest(load_arr[0].url));
			sound.play();
			//Object_pool.sound_obj_pool.put(load_arr[0].url,sound);
			//Sound_manager.getInstance().play_mp3(sound);
			end_init();
		}
		//加载视频 没有用了.
		private function vedio_ld():void
		{
			
		}
		//加载swf img等
		private function content_ld():void
		{
			ld = null;
			GC.nowGC();
			if(!ld)ld = new Loader();
			if(load_arr[0].need_decode)
			{
				data_ld();
			}
			else
			{
				ld_init();
			}
			
		}
		private function ld_init(bytes:ByteArray=null):void
		{
			if(load_arr[0].need_decode)ld.loadBytes(bytes,System_set.getInstance().cn);
			else ld.load(new URLRequest(load_arr[0].url),System_set.getInstance().cn);
			ld.contentLoaderInfo.addEventListener(Event.COMPLETE,ld_cHandler);
			ld.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,pro_Handler);
			ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,io_Handler);
		}
		//xml txt等数据加载完成
		private function url_cHandler(e:Event):void
		{
			urlld.removeEventListener(Event.COMPLETE,url_cHandler);
			urlld.removeEventListener(ProgressEvent.PROGRESS,pro_Handler);
			urlld.removeEventListener(IOErrorEvent.IO_ERROR,io_Handler);
			if(load_arr[0].need_decode)
			{
				var bytes:ByteArray = ByteArray(urlld.data);
				bytes.uncompress()
				if(type == Loading_data.type_xml)
				{
					urlld.data = bytes.readUTFBytes(bytes.bytesAvailable);
				}
				else if(type == Loading_data.type_swf || type == Loading_data.type_img)
				{
					ld_init(bytes);
					return;
				}
			}
			Object_pool.loaded_obj_pool.put(load_arr[0].url,urlld.data);
			load_arr[0].callback(urlld.data);
			end_init();
		}
		//content数据加载完成
		private function ld_cHandler(e:Event):void
		{
			ld.contentLoaderInfo.removeEventListener(Event.COMPLETE,ld_cHandler);
			ld.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,pro_Handler);
			ld.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,io_Handler);
			Object_pool.loaded_obj_pool.put(load_arr[0].url,ld);
			load_arr[0].callback(ld);
			end_init();
		}
		//数据加载中
		private function pro_Handler(e:ProgressEvent):void
		{
			loading_big_mc.show_change(load_arr[0].des,Math.floor(e.bytesLoaded / e.bytesTotal * 100 ));
		}
		//数据加载错误
		private function io_Handler(e:IOErrorEvent):void
		{
			Debug.log(ld.contentLoaderInfo.url);
			Global_data.show_game_des.show(ld.contentLoaderInfo.url+"is not found!");
			end_init();
		}
		//处理一个对象加载完成
		private function end_init():void
		{
			load_arr.shift();
			loading_bool = false;
			var len:int = load_arr.length;
			if(len !=0)real_start();
			else
			{
				Pop_win_manager.getInstance().Remove_pop_win(loading_big_mc,false);//移除loading条
				first_bool = true;
			}
		}
	}
}